import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores/user.js'
import { showNotify } from 'vant'
import 'vant/es/notify/style'
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      component: () => import('@/views/home/layout.vue'),
      children: [
        {
          path: '/',
          redirect: '/home'
        },
        {
          path: '/home',
          name: 'home',
          component: () => import('@/views/home/home/home.vue')
        },
        {
          path: '/category',
          name: 'category',
          component: () => import('@/views/home/category/category.vue')
        },
        {
          path: '/cart',
          name: 'cart',
          component: () => import('@/views/home/cart/cart.vue')
        },
        {
          path: '/mine',
          name: 'mine',
          component: () => import('@/views/home/mine/mine.vue')
        }
      ]
    },
    {
      path: '/detail/:id?',
      name: 'detail',
      component: () => import('@/views/detail/detail.vue')
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('@/views/login/login.vue')
    },
    {
      path: '/preview',
      name: 'preview',
      component: () => import('@/views/preview/preview.vue')
    },
    {
      path: '/address',
      name: 'address',
      component: () => import('@/views/address/address.vue')
    },
    {
      path: '/addEdit/:id?',
      name: 'addEdit',
      component: () => import('@/views/address/addEdit.vue')
    },
    {
      path: '/pay/:id/:type',
      name: 'pay',
      component: () => import('@/views/pay/pay.vue')
    },
    {
      path: '/orderDetail/:id',
      name: 'orderDetail',
      component: () => import('@/views/orderDetail/orderDetail.vue')
    },
    {
      path: '/mineOrder',
      name: 'mineOrder',
      component: () => import('@/views/mineOrder/mineOrder.vue')
    },
    {
      path: '/collection',
      name: 'collect',
      component: () => import('@/views/collect/collect.vue')
    },
    {
      path: '/person',
      name: 'person',
      component: () => import('@/views/person/person.vue')
    }
  ]
})

const authRoute = ['/pay', '/cart', '/mine', '/preview']

router.beforeEach((to) => {
  const userStore = useUserStore()
  if (authRoute.includes(to.path) && !userStore.token)
    return (
      showNotify({
        type: 'danger',
        message: '登陆后才能查看哟'
      }) && { name: 'login' }
    )
  return true
})

export default router
